package untiring.controllers;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import untiring.interfaces.GalleryService;
import untiring.models.GalleryVO;


@Controller
@RequestMapping("/gallery/*")
public class GalleryController{
	
	@Autowired
	private GalleryService galleryService;

	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(Model model) throws Exception{
		model.addAttribute("gallerys", galleryService.readGallery());
		return "gallery/list";
	}
	@RequestMapping(value="/view", method = RequestMethod.GET)
	public String galleryview(Model model,@RequestParam String gno) throws Exception{
		model.addAttribute("gallery", galleryService.readOneGallery(gno));
		return "gallery/view";
	}
	
	
	@RequestMapping(value="/write", method = RequestMethod.GET)
	public String writeget() throws Exception{
		return "gallery/write";
	}
	
	@RequestMapping(value="/write", method = RequestMethod.POST)
	public String writepost(@ModelAttribute GalleryVO g) throws Exception{
		galleryService.manageImage(g);
		return galleryService.newGallery(g);
	}
	
	@RequestMapping(value="/update", method = RequestMethod.GET)
	public String updateget() throws Exception{
		return "gallery/update";
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public String updatepost() throws Exception{
		String viewPage =  "gallery/update";
		viewPage = "redirect:/gallery/update.mvc";
		return viewPage;
	}
	@RequestMapping(value="/deleteok", method = RequestMethod.GET)
	public String delete() throws Exception{
		return "redirect:/gallery/list.mvc";
	}
	
	@RequestMapping("/image")
	   @ResponseBody
	   public byte[] image(@ModelAttribute GalleryVO g, HttpServletResponse res) throws Exception {      
	      return galleryService.viewImage(g, res);
	   }
	
}
